#include "CGame.h"
#include "States\CMainMenuState.h"
#include "Managers\CInputManager.h"

CGame::CGame()
{
	m_pMainMenu = NULL;
}
CGame::~CGame()
{

}

bool CGame::isWindowOpen()
{
	return m_Window->isOpen();
}

bool CGame::Init(sf::RenderWindow &window)
{
	m_Window = &window;
	
	if( !m_Font.loadFromFile("resource/Fonts/framd.ttf"))
		return false;
	m_Text = sf::Text("Hello world", m_Font, 50);
	m_Text.setColor(sf::Color(0,255,0,255));
	m_Text.setScale(2.0f, 2.0f);

	m_InputText.setFont(m_Font);
	m_InputText.setCharacterSize(20);
	m_InputText.setColor(sf::Color(255, 0, 0, 255));

	m_cInputManager = new CInputManager();
	if(!m_cInputManager->Init(window))
	{
		return false;
	}

	
	m_pMainMenu = new CMainMenuState(window, *m_cInputManager);
	m_pMainMenu->Enter();

	return true;
}

void CGame::Update(sf::Time timeElapsed)
{
	HandleEvents();
	char buffer[255] = "";
	sprintf_s(buffer, "%f", timeElapsed.asSeconds());
	sf::String str = sf::String(buffer);

	//Use this to show the ellapsed time
	// We can add the option to show this in some way as FPS
	//m_Text.setString(str);


	// This was a test to see how scaling would work over time
	// Uncomment the lines to see what it does
	//float velTime = 500 * timeElapsed.asSeconds();
	//m_Text.setScale(m_Text.getScale() + (m_Text.getScale() * velTime));

	m_Text.setPosition((float)m_cInputManager->GetMouseState().m_Pos.x, (float)m_cInputManager->GetMouseState().m_Pos.y);

	m_pMainMenu->Update(timeElapsed);
}

bool CGame::Input()
{
	// Testing my input
	if(m_cInputManager)
	{		
		m_pMainMenu->Input();
	}

	return true;
}

void CGame::Render()
{
	m_Window->clear(sf::Color(0,0,255,255));

	m_Window->draw(m_Text);
	m_Window->draw(m_InputText);

	m_pMainMenu->Render();
	
	m_Window->display();
}

void CGame::Shutdown()
{
	if(m_cInputManager)
	{
		delete m_cInputManager;
		m_cInputManager = NULL;
	}

	if(m_pMainMenu)
	{
		m_pMainMenu->Exit();
		delete m_pMainMenu;
		m_pMainMenu = NULL;
	}
}

void CGame::HandleEvents()
{
	sf::Event event;
	while(m_Window->pollEvent(event))
	{
		// If close then close the window
		if( event.type == sf::Event::Closed)
		{
			m_Window->close();
		}
		else if(event.type == sf::Event::TextEntered)
		{
			
			if(m_cInputManager->GetPressedKey().m_Key == sf::Keyboard::Back)
			{
				if(!m_InputText.getString().isEmpty())
				{
					sf::String newString = m_InputText.getString();
					newString.erase(newString.getSize()-1);
					m_InputText.setString(newString);
				}
			}
			else if(m_cInputManager->GetPressedKey().m_Key == sf::Keyboard::Return)
			{
				m_InputText.setString(m_InputText.getString() + '\n');
			}
			else
			{
				m_InputText.setString(m_InputText.getString() + event.text.unicode);
			}

			
		}
		else if(event.type == sf::Event::KeyPressed)
		{
			if(event.key.code == sf::Keyboard::Escape)
			{
				CloseWindow();
			}
			
		}
	}
	
}

void CGame::CloseWindow()
{
	m_Window->close();
}
